package com.leyou.user.controller;

import com.leyou.common.exceptions.LyException;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.stream.Collectors;

@RestController
public class UserController {
//    GET /check/{data}/{type}


    @Autowired
    private UserService userService;

    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkData(
            @PathVariable("data") String data,
            @PathVariable("type") Integer type
    ){
       Boolean b = userService.checkData(data,type);
       return ResponseEntity.ok(b);
    }

    @GetMapping("/check1/{data}/{type}")
    @ApiOperation(value = "校验用户名或手机号码数据是否可用，如果返回true则可用") //对此方法的说明
    @ApiResponses({
            @ApiResponse(code = 200, message = "校验结果有效，true或false代表可用或不可用"),
            @ApiResponse(code = 400, message = "请求参数有误，比如type不是指定值")
    })
    public ResponseEntity<Boolean> checkUserData1(
            @ApiParam(value = "要校验的数据", example = "lisi") @PathVariable("data") String data,
            @ApiParam(value = "数据类型，1：用户名，2：手机号", example = "1") @PathVariable(value = "type") Integer type) {
        return ResponseEntity.ok(userService.checkData(data, type));
    }

    @PostMapping("/code")
    public ResponseEntity<Boolean> sendCode(@RequestParam("phone") String phone){
       userService.sendCode(phone);
       return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user, BindingResult result, @RequestParam("code") String code){
        if(result.hasErrors()){ //判断是否有错误
            String errorMessage = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining("|"));
//       errorMessage =  密码格式不正确|用户名格式不正确
            throw new LyException(404,errorMessage);
        }
        userService.register(user,code);
       return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @GetMapping("/query")
    public ResponseEntity<UserDTO> queryByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password){
        UserDTO userDTO  = userService.queryByUsernameAndPassword(username,password);
       return ResponseEntity.ok(userDTO);
    }

}
